import logging
from aiogram import Dispatcher, executor
from aiogram.contrib.fsm_storage.redis import RedisStorage2

from app.handlers import setup as setup_handlers
from app.bot_init import bot
from app.services.db import check_db_exists

check_db_exists()
storage = RedisStorage2()
dp = Dispatcher(bot, storage=storage)

#TODO вынести логер в отдельный модуль?
LOG_FORMAT = "%(asctime)s %(levelname)s %(name)s - %(message)s"
logging.basicConfig(
		#filename="program.log",
		level=logging.DEBUG,
		format=LOG_FORMAT)
logger = logging.getLogger(__name__)

logging.getLogger("aiogram").setLevel(logging.DEBUG + 1)


def main():
    setup_handlers(dp)
    logger.info("Bot has been started ...")
    executor.start_polling(dp, skip_updates=True)

if __name__ == "__main__":
    try:
        main()
    except Exception:
        logger.exception('Main exception')
    finally:
        pass
